package de.cube3d.blitwit.test 
{
	import de.cube3d.blitwit.render.BlitSpriteScene;
	import flash.geom.Point;
	import flash.display.Graphics;
	import flash.utils.getTimer;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.display.BitmapData;
	import de.cube3d.blitwit.render.*;
	import de.cube3d.blitwit.space.*;

	
	/**
	 * The test class is a base class for simple runtime tests to show off numbers to the
	 * user. It provides a simple interface function to handle keypresses.
	 * 
	 */
	public class Test 
	{
		public var scene:BlitSpriteScene = new BlitSpriteScene();
		protected var _base:Array = ["grass_hill", "grass_hill_2", "grass_hill_3_1",
			"grass_hill_3_2", "grass_hill_3_3", "grass_hill_3_4"];

		
		public function Test() 
		{
			scene = new BlitSpriteScene();
			init();
		}
		protected function init():void {
			
		}
		
		public function renderTick(t:Number):void {
			
		}
		
		/**
		 * shown to the user each frame.
		 * @return
		 */
		public function getStats():String {
			return "drawtime: " + (_lastRenderTime) + "ms\n" +
				"selected object count: " + scene.statsVisibleObjectCount + "\n"+
				"actual drawcalls: " + scene.statsDrawCalls + "\n"+
				"objects in scene: " + scene.objectCount + "\n" +
				"selectd: "+_selected;
		}
		
		protected var _t:int = 0;
		protected var _lastRenderTime:int = 0;
		public function render (graphics:Graphics, stageWidth:int, stageHeight:int):void {
			var t:int = getTimer();
			var dt:int = (_t?t - _t:0);
			_t = t;
			
			if (scene.width != stageWidth || scene.height != stageHeight) {
				scene.setDimension(stageWidth, stageHeight);
				graphics.clear();
				graphics.beginBitmapFill(scene.bitmapData);
				graphics.drawRect(0, 0, scene.bitmapData.width, scene.bitmapData.height);
				graphics.endFill();
			}
			scene.panBy(int(_pan.x * dt * .1) * .25, int(_pan.y * dt * .1) * .25);
			renderTick(t);
			scene.animationTick(t);
			scene.updateImage();
			_lastRenderTime = getTimer() - t;
		}
		private var _pan:Point = new Point();
		
		public function keyPressed(event:KeyboardEvent):void {
			switch (event.keyCode) {
				case 37: _pan.x = -4; break;// left
				case 38: _pan.y = -4; break;// up
				case 39: _pan.x = 4; break; // right;
				case 40: _pan.y = 4; break; // down
				default: trace(event.keyCode);
			}
		}
		public function keyReleased(event:KeyboardEvent):void {
			switch (event.keyCode) {
				case 37: _pan.x = 0; break;// left
				case 38: _pan.y = 0; break;// up
				case 39: _pan.x = 0; break; // right;
				case 40: _pan.y = 0; break; // down
			}
		}
		public function clearKeys(event:Event):void {
			_pan.x = _pan.y = 0;
		}
		
		private var _list:Vector.<SpaceObject> = new Vector.<SpaceObject>();
		protected var _selected:SpaceObject;
		protected var selectspaces:Vector.<Space>;
		public function mouseMoved(x:int, y:int):void {
			_list.length = 0;
			scene.getObjectsAt(x, y, _list, selectspaces);
			if (_list.length == 0) {
				_selected = null;
				return;
			}
			_selected = (_list[_list.length-1]);
		}
		public function mouseDown (x:int, y:int):void {
			
		}
		public function mouseUp (x:int, y:int):void {
			
		}
	}

}